// pages/active-info/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page: {
          searchCount: false,
          current: 1,
          size: 10,
        },
        name:'',
        swiperData: [
          '../../public/img/pc1.png',
          '../../public/img/pc2.png',
          '../../public/img/pc3.png',
        ],
        curIndex:0,
        cateList: [],
        active:[],
        userInfo:''
    },
    onShow() {
      app.initPage().then(res => {
        this.setData({
          userInfo: res
        })
        this.setData({active:[]})
        this.getCate()
      })
    },
    onChange(e){
        this.setData({name:e.detail})
    },
    toDetail(e){
      if(this.data.userInfo.auths == 0){
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '此功能仅对星程计划学员开放，详细请咨询【启航星程SV小助手】（微信号：starvoyage01）',
          complete: (res) => { 
          }
        })
        return
      }
      var id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: `/pages/wish/ActiveDetail/index?id=${id}`,
      })
    },
    onClick(){
        this.setData({active:[]})
        this.getList()
    },
    getCate(){
      app.api.getWishCate({id:2}).then(res => {
        res.data.unshift({id:0,name:'全部'})
        this.setData({
          cateList: res.data
        })
        this.getList()
      })
    },
    getList(){
      app.api.getActiveList({
        searchCount: false,
        current: this.data.page.current,
        size: this.data.page.size,
        cateId: this.data.curIndex == 0 ? ''  
                : this.data.cateList[this.data.curIndex].id,
        title: this.data.name
        // descs: 'create_time'
      })
        .then(res => {  
          res.data.records.forEach(item => {
            item.createTime = item.createTime.substring(0,10)
          })
          let active = res.data.records
          this.setData({
            active: [...this.data.active, ...active]
          })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    
     // 切换tab
  cateSwitch(e){
    this.setData({
      ['page.current']: 1,
      curIndex: e.currentTarget.dataset.index,
      active:[]
    })
    this.getList()
    
  },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
      this.setData({
        ['page.current']: this.data.page.current + 1
      })
      this.getList()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})